using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using Utility = PHSRAG.Utility;
using PHSRAG.Utility;
using System.Text;
using System.Web.Script.Services;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace PHSRAG.Insight21
{
    /// <summary>
    /// Summary description for CommonLookup
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class Log4Net : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet=false, ResponseFormat = ResponseFormat.Json)]
        public FlexiGridJSON GetEntries(int page, int rp, string sortname, string sortorder, string query, string qtype)
        {
            try
            {
                SqlParameter[] sqlParams = DataAccess.BuildSqlParameterArray(
                    "@pageNumber", SqlDbType.Int, page,
                    "@rowsToFetch", SqlDbType.Int, rp,
                    "@totalRowCount", SqlDbType.Int, 0,
                    "@sortColumn", SqlDbType.VarChar, sortname,
                    "@sortAscending", SqlDbType.Bit, sortorder == "asc");

                if (!string.IsNullOrEmpty(query))
                {
                    switch (qtype)
                    {
                        case "MinDate":
                            DataAccess.AppendToSqlParameterArray(ref sqlParams, "@MinDate", SqlDbType.DateTime, query);
                            break;
                        case "MaxDate":
                            DataAccess.AppendToSqlParameterArray(ref sqlParams, "@MaxDate", SqlDbType.DateTime, query);
                            break;
                        case "Level":
                            DataAccess.AppendToSqlParameterArray(ref sqlParams, "@Level", SqlDbType.VarChar, query);
                            break;
                        case "Logger":
                            DataAccess.AppendToSqlParameterArray(ref sqlParams, "@Logger", SqlDbType.VarChar, query);
                            break;
                        case "Message":
                            DataAccess.AppendToSqlParameterArray(ref sqlParams, "@Message", SqlDbType.VarChar, query);
                            break;
                        case "Exception":
                            DataAccess.AppendToSqlParameterArray(ref sqlParams, "@Exception", SqlDbType.VarChar, query);
                            break;
                    }
                }

                DataSet ds = DataAccess.GetDataSet(DataAccess.ConnectionString, 
                    "GetLog4NetEntries",
                    sqlParams);

                // Fill objects which the flexigrid expects
                FlexiGridJSON json = new FlexiGridJSON();
                json.page = page;
                json.total = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    FlexiGridRow fgrow = new FlexiGridRow();
                    fgrow.id = Convert.ToInt32(row[0]);
                    foreach (object item in row.ItemArray)
                    {
                        fgrow.cell.Add(HttpUtility.HtmlEncode(item.ToString()));
                    }
                    json.rows.Add(fgrow);
                }

                // Due to the ResponseFormat attribute, this object will be serialized to JSON
                // before being returned to the client
                return json;
            }
            catch
            {
                return new FlexiGridJSON();
            }
        }

        [WebMethod]
        [ScriptMethod]
        public object[] GetEntry(int id)
        {
            DataSet ds = DataAccess.GetDataSet(DataAccess.ConnectionString,
                "GetLog4NetEntry",
                DataAccess.BuildSqlParameterArray(
                "@id", SqlDbType.Int, id));

            return ds.Tables[0].Rows[0].ItemArray;
        }
    }

    public class FlexiGridJSON
    {
        public int page;
        public int total;
        public List<FlexiGridRow> rows = new List<FlexiGridRow>();
    }

    public class FlexiGridRow
    {
        public int id;
        public List<string> cell= new List<string>();
    }

}